<?php

class Message_Controller_Browse_Add extends Nuo_Controller_Member
{
	/**
	 * @todo support to select sender in some case
	 */
	function execute($request)
	{
		$form = Nuo::getObject('message/add', 'form');

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$data = $form -> getData();

			$userId = $request -> getParam('user_id');
			$receiver = Nuo::getTable('user') -> find($userId) -> current();

			$messageApi = Nuo::getApi('message');
			$poster = Nuo::getUser();
			$message = $messageApi -> addMessage($poster, $title = $data['title'], $body = $data['body'], $attachments = NULL);
			$item = $messageApi -> send($message, $receiver);

			$uri = $poster -> getUrl('message/sent');

			Nuo::redirect($uri);

		}

		return  $form -> __toString();
	}

}
